﻿using DBUtility;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Common;

namespace Business
{
    public class PAY_ADMIN
    {
        #region http上下文
        public static HttpContext Current
        {
            get { return HttpContext.Current; }
        }

        public static HttpRequest Request
        {
            get { return Current.Request; }
        }

        public static HttpResponse Response
        {
            get { return Current.Response; }
        }
        #endregion
        #region model
        public int ID { get; set; }
        public string AdminName { get; set; }
        public string AdminPass { get; set; }
        public string LoginIP { get; set; }
        public DateTime LoginTime { get; set; }
        #endregion

        #region Function
        public PAY_ADMIN() { }
        public PAY_ADMIN Get(int ID)
        {
            PAY_ADMIN PAY_ADMIN = null;
            MySqlDataReader sdr = null;
            using (sdr = MYSQL.ExecuteReader(string.Format("select * From PAY_ADMIN where ID={0}", ID)))
            {
                if (sdr.Read())
                {
                    PAY_ADMIN = GetData(PAY_ADMIN, sdr);
                }
            }
            sdr.Close();
            return PAY_ADMIN;
        }
        public PAY_ADMIN SelectByInt(String where, int value)
        {
            PAY_ADMIN PAY_ADMIN = null;
            MySqlDataReader sdr = null;
            using (sdr = MYSQL.ExecuteReader(string.Format("select * From PAY_ADMIN where {0}={1} ", where, value)))
            {
                while (sdr.Read())
                {
                    PAY_ADMIN = GetData(PAY_ADMIN, sdr);
                }
            }
            sdr.Close();
            return PAY_ADMIN;
        }
        public PAY_ADMIN SelectByStr(String where, String value)
        {
            PAY_ADMIN PAY_ADMIN = null;
            MySqlDataReader sdr = null;
            using (sdr = MYSQL.ExecuteReader(string.Format("select * From PAY_ADMIN where {0}='{1}' ", where, value)))
            {
                while (sdr.Read())
                {
                    PAY_ADMIN = GetData(PAY_ADMIN, sdr);
                }
            }
            sdr.Close();
            return PAY_ADMIN;
        }
        public PAY_ADMIN SelectByObj(String where)
        {
            PAY_ADMIN PAY_ADMIN = null;
            MySqlDataReader sdr = null;
            using (sdr = MYSQL.ExecuteReader(string.Format("select * From PAY_ADMIN where 1=1 and {0} ", where)))
            {
                while (sdr.Read())
                {
                    PAY_ADMIN = GetData(PAY_ADMIN, sdr);
                }
            }
            sdr.Close();
            return PAY_ADMIN;
        }
        public List<PAY_ADMIN> GetALL()
        {
            PAY_ADMIN PAY_ADMIN = null;
            MySqlDataReader sdr = null;
            List<PAY_ADMIN> list = new List<PAY_ADMIN>();
            using (sdr = MYSQL.ExecuteReader("select * From PAY_ADMIN"))
            {
                while (sdr.Read())
                {
                    PAY_ADMIN = GetData(PAY_ADMIN, sdr);
                    list.Add(PAY_ADMIN);
                }
            }
            sdr.Close();
            return list;
        }
        public List<PAY_ADMIN> GetALL(String where)
        {
            PAY_ADMIN PAY_ADMIN = null;
            MySqlDataReader sdr = null;
            List<PAY_ADMIN> list = new List<PAY_ADMIN>();
            using (sdr = MYSQL.ExecuteReader(string.Format("select * From PAY_ADMIN where 1=1 and {0}", where)))
            {
                while (sdr.Read())
                {
                    PAY_ADMIN = GetData(PAY_ADMIN, sdr);
                    list.Add(PAY_ADMIN);
                }
            }
            sdr.Close();
            return list;
        }
        /// <summary>
        /// 获取前几个
        /// </summary>
        /// <param name="where"></param>
        /// <param name="top"></param>
        /// <returns></returns>
        public List<PAY_ADMIN> GetALL(String where, int top)
        {
            PAY_ADMIN PAY_ADMIN = null;
            MySqlDataReader sdr = null;
            List<PAY_ADMIN> list = new List<PAY_ADMIN>();
            using (sdr = MYSQL.ExecuteReader(string.Format("select top " + top + " * From PAY_ADMIN where 1=1 and {0}", where)))
            {
                while (sdr.Read())
                {
                    PAY_ADMIN = GetData(PAY_ADMIN, sdr);
                    list.Add(PAY_ADMIN);
                }
            }
            sdr.Close();
            return list;
        }
        public List<PAY_ADMIN> GetList(String where)
        {
            PAY_ADMIN PAY_ADMIN = null;
            MySqlDataReader sdr = null;
            List<PAY_ADMIN> list = new List<PAY_ADMIN>();
            using (sdr = MYSQL.ExecuteReader(string.Format("{0}", where)))
            {
                while (sdr.Read())
                {
                    PAY_ADMIN = GetData(PAY_ADMIN, sdr);
                    list.Add(PAY_ADMIN);
                }
            }
            sdr.Close();
            return list;
        }
        public bool Insert(ref PAY_ADMIN PAY_ADMIN)
        {
            try
            {
                Current.Application.Lock();
                MySqlParameter[] p = new MySqlParameter[]{
                new MySqlParameter("@adminname",PAY_ADMIN.AdminName),
                new MySqlParameter("@adminpass",PAY_ADMIN.AdminPass),
                new MySqlParameter("@loginip",PAY_ADMIN.LoginIP),
                new MySqlParameter("@logintime",PAY_ADMIN.LoginTime),
            };
                int i = Convert.ToInt32(MYSQL.ExecuteSql(string.Format("insert into PAY_ADMIN values(null,@adminname,@adminpass,@loginip,@logintime)"), p));
                return i == 0 ? false : true;
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                Current.Application.UnLock();
            }
        }
        public bool Update(PAY_ADMIN PAY_ADMIN)
        {
            MySqlParameter[] p = new MySqlParameter[]{
                new MySqlParameter("@ID",PAY_ADMIN.ID),
                new MySqlParameter("@adminname",PAY_ADMIN.AdminName),
                new MySqlParameter("@adminpass",PAY_ADMIN.AdminPass),
                new MySqlParameter("@loginip",PAY_ADMIN.LoginIP),
                new MySqlParameter("@logintime",PAY_ADMIN.LoginTime)
            };
            int i = MYSQL.ExecuteSql(string.Format("update PAY_ADMIN set ID=@ID,adminname=@adminname,adminpass=@adminpass,loginip=@loginip,logintime=@logintime where ID=@ID"), p);
            return i == 0 ? false : true;
        }
        public bool Delete(int ID)
        {
            int i = MYSQL.ExecuteSql(string.Format("delete from PAY_ADMIN where ID={0}", ID));
            return i == 0 ? false : true;
        }
        private PAY_ADMIN GetData(PAY_ADMIN PAY_ADMIN, MySqlDataReader sdr)
        {
            PAY_ADMIN = new PAY_ADMIN();
            PAY_ADMIN.ID = (int)sdr["ID"];
            PAY_ADMIN.AdminName = (string)sdr["adminname"];
            PAY_ADMIN.AdminPass = (string)sdr["adminpass"];
            PAY_ADMIN.LoginIP = (string)sdr["loginip"];
            PAY_ADMIN.LoginTime = Convert.ToDateTime(sdr["logintime"]);
            return PAY_ADMIN;
        }
        /// <summary>
        /// 将DataTable数据转换成实体类列表
        /// 郑科 
        /// 日期：2016年11月4日10.18
        /// </summary>
        /// <param name="dt"></param>
        /// <returns>List<School></returns>
        public List<PAY_ADMIN> ParseToList(DataTable dt)
        {
            if (dt == null || dt.Rows.Count < 1)
            {
                return null;
            }
            List<PAY_ADMIN> PAY_ADMIN_list = new List<PAY_ADMIN>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                PAY_ADMIN PAY_ADMIN = new PAY_ADMIN();
                PAY_ADMIN.ID = int.Parse(dt.Rows[i]["ID"].ToString());
                PAY_ADMIN.AdminName = dt.Rows[i]["adminname"].ToString();
                PAY_ADMIN.AdminPass = dt.Rows[i]["adminpass"].ToString();
                PAY_ADMIN.LoginIP = dt.Rows[i]["loginip"].ToString();
                PAY_ADMIN.LoginTime = Convert.ToDateTime(dt.Rows[i]["logintime"]);
                PAY_ADMIN_list.Add(PAY_ADMIN);
            }
            return PAY_ADMIN_list;
        }
        #endregion
        #region join
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="pageindex">第几页</param>
        /// <param name="pagesize">一页列表数量</param>
        /// <param name="recordcount">记录总数</param>
        /// <returns></returns>
        public List<PAY_ADMIN> PagingQueryList(PageTable pageTable, out int recordcount)
        {
            recordcount = MYSQL.GetInt("select count(*) from PAY_ADMIN where " + pageTable.Where);
            return ParseToList(MYSQL.GetDataTable(PageSql.GetPage(pageTable)));
        }
        #endregion
    }
}
